package cn.kgc.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.kgc.bean.dto.UserDto;
import cn.kgc.bean.pojo.CUser;
import cn.kgc.service.GMUserService;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class GMUserController {

    @Resource
    private GMUserService gmUserService;

    //分页查询用户\
    @GetMapping("GMUser")
    public String findPage(@RequestParam(defaultValue = "1",value = "pageNum")  int pageNum,
                           @RequestParam(defaultValue = "5",value = "pageSize") int pageSize)
    {
        PageHelper.startPage(pageNum,pageSize);
        List<UserDto> list = gmUserService.selectAllCuser();
        PageInfo<UserDto> pageInfo = new PageInfo<>(list);
        String fenye = JSONArray.toJSONString(pageInfo);
        return fenye;


    }
    //添加用户
    @PostMapping("/add")
    public String addCuser(CUser cUser){
        this.gmUserService.addCUser(cUser);
        return "succes";
    }

    //修改用户
    @PostMapping("/updateBywordId")
    public String updateBywordId(@RequestBody CUser cUser){
        if (cUser.getId()!=null){
            this.gmUserService.updateCUserByworkId(cUser);
        }else {
            this.gmUserService.addCUser(cUser);
        }
        return "succes";
    }
    //删除用户
    @DeleteMapping("/deleteCuser/{id}")
    public String deleteCuser(@PathVariable Long id){
       this.gmUserService.deleteCUserByworkId(id);
       return "succes";
    }

    //导出接口
    @GetMapping("/export")
    public void export(HttpServletResponse response)throws Exception{
        // 从数据里面查询所有数据
        List<UserDto>list = gmUserService.selectAllCuser();
        //通过工具类创建writer 写出到磁盘路径
        // EXcelWriter writer = ExceUtil.getWriter(filesUploadPath+"/用户信息.xlsx");
        //在内存操作，写出到浏览器
        ExcelWriter writer = new ExcelWriter(true);
        // 一次性写出list内的对象到excel，使用默认样式，强制输出标题
        writer.write(list,true);
        // 设置浏览器响应格式
        response.setContentType("application/vnd.openxmlformate-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("用户信息","UTF-8");
        response.setHeader("Content-Disposition","attachment;filename="+fileName+".xlsx");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out,true);
        out.close();
        writer.close();

    }
    //导入
    @PostMapping("/import")
    public Boolean imp(MultipartFile file)throws Exception{
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        List<UserDto> list = reader.readAll(UserDto.class);
//        gmUserService.saveBatch(list);
        gmUserService.selectList(list);
        return true;
    }


}
